package com.three.vip.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.three.core.entity.User;
import com.three.core.entity.UserIntegral;
import com.three.core.entity.UserIntegralLog;
import com.three.core.vo.R;
import com.three.vip.feign.AdminUserFeign;
import com.three.vip.mapper.UserIntegralLogMapper;
import com.three.vip.mapper.UserIntegralMapper;
import com.three.vip.service.UserIntegralService;
import com.three.vip.vo.CustomerSearchFormVo;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author ：JinShiYan
 * @date ：Created in 2025/3/12 20:40
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class UserIntegralServiceImpl implements UserIntegralService {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserIntegralMapper userIntegralMapper;

    @Autowired
    private AdminUserFeign adminUserFeign;

    @Autowired
    private UserIntegralLogMapper userIntegralLogMapper;

    @Override
    public R<IPage<UserIntegralLog>> PointManagement(CustomerSearchFormVo customerSearchFormVo) {
        //获取Token
        String token = request.getHeader("token");
        // 获取用户名
        Object user = redisTemplate.opsForHash().get("user::login::" + token, "name");
        R<User> byName = adminUserFeign.getByName((String) user);
        Integer userId = byName.getData().getUserId();
        Page<UserIntegralLog> page = new Page<>(customerSearchFormVo.getCurrentPage(), customerSearchFormVo.getPageSize());

        IPage<UserIntegralLog> productOrderIPage = userIntegralLogMapper.selectByCondition(page, customerSearchFormVo.getUserName(), userId);
        return new R<>(200, "查询成功", productOrderIPage);

    }

    @Override
    public R<UserIntegral> selectBalanceByUserId(Integer id) {
        UserIntegral userIntegral = userIntegralMapper.selectBalanceByUserId(id);
        return new R<>(200, "查询成功", userIntegral);
    }
}
